package com.amorcloud.pig.activity;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.amorcloud.pig.R;
import com.amorcloud.pig.utils.SharedPreferencesUtils;

public class ServerConfigActivity extends BaseActivity implements View.OnClickListener {

    EditText mFarmName, mFarmIp, mFarmPort, mFarmId;
    Button mSave;
    String farmName, farmIp, farmPort, farmId = "";

    @Override
    protected void init() {
        setContentView(R.layout.activity_server_conf);
        initCustomToolbar(this);
        initView();
        initData();
    }

    private void initView() {
        mFarmName = findViewById(R.id.etv_farm_name);
        mFarmIp = findViewById(R.id.etv_farm_ip);
        mFarmPort = findViewById(R.id.etv_farm_port);
        mSave = findViewById(R.id.btn_serverconf_save);
        mSave.setOnClickListener(this);
        mFarmId = findViewById(R.id.tv_farm_id);
    }

    private void initData() {
        farmName = (String) SharedPreferencesUtils.getParam("farmName", "");
        farmIp = (String) SharedPreferencesUtils.getParam("farmIp", "");
        farmPort = (String) SharedPreferencesUtils.getParam("farmPort", "");
        farmId = (String) SharedPreferencesUtils.getParam("farmId", "");
        if(!TextUtils.isEmpty(farmName)){
            mFarmName.setText(farmName);
        }
        if(!TextUtils.isEmpty(farmId)) {
            mFarmId.setText(farmId);
        }
        if(!TextUtils.isEmpty(farmIp)) {
            mFarmIp.setText(farmIp);
        }
        if(!TextUtils.isEmpty(farmPort)) {
            mFarmPort.setText(farmPort);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_serverconf_save:
                saveConfig();
                break;
            default:
                break;
        }
    }

    private void saveConfig() {
        String farmName = mFarmName.getText().toString();
        String farmId = mFarmId.getText().toString();
        String farmIp = mFarmIp.getText().toString();
        String farmPort = mFarmPort.getText().toString();
        Log.d("leihuiming", "farmName = " + farmName + " farmIp= " + farmIp + " farmPort=" + farmPort);
        if (!TextUtils.isEmpty(farmName)) {
            SharedPreferencesUtils.setParam("farmName", farmName);
        }
        if(!TextUtils.isEmpty(farmId)) {
            SharedPreferencesUtils.setParam("farmId", farmId);
        }
        if (!TextUtils.isEmpty(farmIp)) {
            SharedPreferencesUtils.setParam("farmIp", farmIp);
        }
        if (!TextUtils.isEmpty(farmPort)) {
            SharedPreferencesUtils.setParam("farmPort", farmPort);
        }
        Toast.makeText(this,"保存成功", Toast.LENGTH_SHORT).show();
        this.finish();
    }
}
